package com.airlines.crewassignment.controller.crewlocation;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.airlines.base.entity.user.User;
import com.airlines.common.Page;
import com.airlines.common.R;
import com.airlines.crewassignment.entity.crewlocation.DutySchedule;
import com.airlines.crewassignment.service.crewlocation.DutyScheduleService;

/**
 * @author xxxx
 * @date 2018/07/02 16:17:14
 * @company xxxx Airlines
 * @description 
 */
@Controller
@RequestMapping("/dutySchedule")
public class DutyScheduleController {

    private static final String PREFIX = "/crewlocation";

    @Autowired
    DutyScheduleService dutyScheduleService;

    /**
     * 进入页面
     * @return
     */
    @RequestMapping(value = "", method={ RequestMethod.GET})
    //@RequiresPermissions("crewlocation:dutySchedule:list")
    String toDutySchedulePage(String moduleFlag,Model model){
        model.addAttribute("moduleFlag",moduleFlag);
        return PREFIX + "/dutyScheduleList";
    }

    /**
    * 多条件模糊 查询并且分页显示
    * @param dutySchedule
    * @param offset
    * @param limit
    * @return
    */
    @RequestMapping(value = "/list")
    @ResponseBody
   // @RequiresPermissions("crewlocation:dutySchedule:list")
    public Page list(DutySchedule dutySchedule, String offset, String limit) {
        Page page = new Page();
        page.setPageNumber((Integer.valueOf(offset) / (Integer.valueOf(limit))));
        page.setPageSize(Integer.valueOf(limit));
        page = dutyScheduleService.list(dutySchedule, page);
        return page;
    }

    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping(value = "/add", method={ RequestMethod.GET})
   // @RequiresPermissions("crewlocation:dutySchedule:add")
    String add(String moduleFlag,Model model){
        model.addAttribute("moduleFlag",moduleFlag);
        return PREFIX + "/dutyScheduleAdd";
    }

    /**
     * 保存
     * @param dutySchedule
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method={ RequestMethod.POST})
    //@RequiresPermissions("crewlocation:dutySchedule:add")
    public R save(DutySchedule dutySchedule, HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("currentUser");
        dutySchedule.setUpdateBy(user.getWorkcode());
        if(dutyScheduleService.insert(dutySchedule) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 进入编辑页面
     * @param model
     * @param dutySchedule
     * @return
     */
    @RequestMapping(value = "/edit", method={ RequestMethod.GET})
   // @RequiresPermissions("crewlocation:dutySchedule:edit")
    String edit(Model model,DutySchedule dutySchedule){
        model.addAttribute("dutySchedule", dutySchedule);
        return PREFIX + "/dutyScheduleEdit";
    }

    /**
     * 修改
     * @param dutySchedule
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method={ RequestMethod.POST})
   // @RequiresPermissions("crewlocation:dutySchedule:edit")
    public R update(String oldFlightDate,String oldDutyCode,String oldPCode,DutySchedule dutySchedule,HttpServletRequest request){
        DutySchedule oldDutySchedule = new DutySchedule();
        oldDutySchedule.setDutyCode(oldDutyCode);
        oldDutySchedule.setFlightDate(oldFlightDate);
        oldDutySchedule.setPCode1(oldPCode);

        User user = (User)request.getSession().getAttribute("currentUser");
        dutySchedule.setUpdateBy(user.getWorkcode());
        if(dutyScheduleService.update(oldDutySchedule,dutySchedule)>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 进入查看页面
     * @param model
     * @param dutySchedule
     * @return
     */
    @RequestMapping(value = "/view", method={ RequestMethod.GET})
    //@RequiresPermissions("crewlocation:dutySchedule:list")
    String view(Model model,DutySchedule dutySchedule){
        model.addAttribute("dutySchedule", dutySchedule);
        model.addAttribute("isReadonly", 1);
        return PREFIX + "/dutyScheduleEdit";
    }

    /**
     * 删除
     * @param dutySchedule
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method={ RequestMethod.POST})
   // @RequiresPermissions("crewlocation:dutySchedule:delete")
    public R delete( DutySchedule dutySchedule){
        if(dutyScheduleService.delete(dutySchedule)>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 检查备份名称是否存在
     * @param dutySchedule
     * @return
     */
    @RequestMapping("check")
    @ResponseBody
    public R check(DutySchedule dutySchedule){
        boolean flag = false;
        if(StringUtils.isEmpty(dutySchedule.getFlightDate()) || StringUtils.isEmpty(dutySchedule.getDutyCode())){
            flag = true;
        }else{

            List<DutySchedule> dutySchedules = dutyScheduleService.list(dutySchedule);
            flag = dutySchedules.size() == 0;
        }
        return R.ok().put("valid",flag);
    }

}
